package com.FindMyStuff.Activities;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.FindMyStuff.Model.DataAccessor;
import com.FindMyStuff.Model.DataBaseHelper;
import com.FindMyStuff.Model.Item;
import com.FindMyStuff.Model.SessionManager;

/**
 * The main menu of the application
 *
 */
public class HomeActivity extends Activity {
	
	/**
	 * Holds the SessionManager so garbage collection will not get rid of the session manager
	 */
	private SessionManager sm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sm = SessionManager.getInstance();
		
	
		
		//((EditText)findViewById(R.id.search_text)).setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		
	    
	    setContentView(R.layout.activity_home);
		// Show the Up button in the action bar.
	    getActionBar().setDisplayShowHomeEnabled(true);
		getActionBar().setDisplayShowTitleEnabled(false);
	    
	    getActionBar().setDisplayHomeAsUpEnabled(false);
	    getActionBar().setHomeButtonEnabled(false);
		//getActionBar().setTitle("Home");
		getActionBar().setIcon(getResources().getIdentifier("ic_logo", "drawable",  getPackageName()));
				
		
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        
        //Set up list view
        SessionManager sm = SessionManager.getInstance();
		DataBaseHelper dbh = new DataBaseHelper(this);
		DataAccessor da = new DataAccessor(dbh);
				
		ArrayList<Item> values = da.getItemsByUser(sm.getCurrentUser());

		ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
				android.R.layout.simple_list_item_1, values);
		
		ListView list=(ListView)findViewById(android.R.id.list);
		list.setAdapter(adapter);
        
        
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_home, menu);
		
		//search keyboard action
		EditText searchText = (EditText) findViewById(R.id.search_text);
		
		
		searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
		    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
		        	EditText searchBox = (EditText) findViewById(R.id.search_text);
					Intent intent = new Intent(getBaseContext(), SearchActivity.class);
					intent.putExtra("seachText", searchBox.getText().toString());
					startActivity(intent);
					overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
					
					//lower keyboard
					/*InputMethodManager imm = (InputMethodManager)getSystemService(
						      Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
					*/
		            return true;
		        }
		        return false;
		    }
		});
		
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			//Intent toLogin = new Intent(this, LogInActivity.class);
			//startActivity(toLogin);
			//overridePendingTransition  (R.anim.left_slide_out, R.anim.left_slide_in);
			//finish();
			return true;
		case R.id.menu_logout:
			Intent intent = new Intent(this, LogInActivity.class);
			sm.logout();
			startActivity(intent);
			overridePendingTransition  (R.anim.left_slide_out, R.anim.left_slide_in);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	/**
	 * Takes the user to create a new lost item
	 * @param view
	 */
	public void newCreateItemActivity(View view) {
		Intent intent = new Intent(this, AddItemActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
	}
	
	/**
	 * Logs the user out of the application
	 * @param view
	 */
	public void logout(View view){
		Intent intent = new Intent(this, LogInActivity.class);
		sm.logout();
		startActivity(intent);
	}
	
	/**
	 * Takes the user to their Items
	 * @param view
	 */
	public void myItems(View view){
		Intent intent = new Intent(this, UserItemsActivity.class);
		intent.putExtra("Text", "a");
		startActivity(intent);
		overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
	}
	
	/**
	 * Takes the user to the search page and searches based on the text input
	 * @param view
	 */
	public void searchByName(View view)
	{
		EditText searchBox = (EditText) findViewById(R.id.search_text);
		Intent intent = new Intent(this, SearchActivity.class);
		intent.putExtra("seachText", searchBox.getText().toString());
		startActivity(intent);
		overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
	}
}
